package com.pn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pn.entity.Brand;
import com.pn.entity.Supply;
import com.pn.mapper.BrandMapper;
import com.pn.service.BrandService;
import com.pn.utils.PageBean;
import com.pn.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@CacheConfig(cacheNames = "com.pn.service.impl.BrandServiceImpl")
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    //显示品牌信息
    @Override
    public PageBean brandPageList(Brand brand) {
        PageHelper.startPage(brand.getPageNum(),brand.getPageSize());
        Page<Brand> page = brandMapper.brandPageList(brand);
        return new PageBean(page.getTotal(),page.getResult());
    }

    //修改品牌
    @CacheEvict(key = "'all:brand'")
    @Override
    public Result brandUpdate(Brand brand) {

        int i = brandMapper.brandUpdate(brand);
        if (i > 0 ){
            return Result.ok("修改品牌成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"修改品牌失败");
    }

    //添加品牌
    @CacheEvict(key = "'all:brand'")
    @Override
    public Result brandAdd(Brand brand) {

        int i = brandMapper.brandInsert(brand);
        if (i > 0 ){
            return Result.ok("添加品牌成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"添加品牌失败");
    }

    //删除品牌
    @CacheEvict(key = "'all:brand'")
    @Override
    public Result brandDelete(Integer brandId) {

        int i = brandMapper.brandDelete(brandId);
        if (i > 0 ){
            return Result.ok("删除品牌成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"删除品牌失败");
    }

    //显示品牌
    @Cacheable(key = "'all:brand'")
    @Override
    public List<Brand> brandList() {
        List<Brand> brandList =  brandMapper.brandList();
        return brandList;
    }
}
